package com.windystory.socketlibrary;

import com.open.net.client.impl.tcp.bio.BioClient;
import com.open.net.client.impl.tcp.nio.NioClient;
import com.open.net.client.impl.udp.bio.UdpBioClient;
import com.open.net.client.impl.udp.nio.UdpNioClient;
import com.open.net.client.structures.BaseMessageProcessor;
import com.open.net.client.structures.IConnectListener;
import com.open.net.client.structures.TcpAddress;
import com.open.net.client.structures.UdpAddress;

public class UDPClient {

    //double-check
    private static volatile UDPClient instance = null;

    private UDPClient() {
    }

    public synchronized static UDPClient getInstance() {
        if (instance == null) {
            synchronized (UDPClient.class) {
                if (instance == null) {
                    instance = new UDPClient();
                }
            }
        }
        return instance;
    }

    ////----------------------NIO-----------------------------

    /**
     * 初始化UdpNioClient
     *
     * @param mConnectResultListener 连接监听器
     * @param mMessageProcessor      信息接受监听
     */
    public UdpNioClient nio_initClient(IConnectListener mConnectResultListener, BaseMessageProcessor mMessageProcessor) {
        return new UdpNioClient(mMessageProcessor, mConnectResultListener);
    }

    /**
     * 设置ip端口
     *
     * @param mClient NioClient
     * @param ip      ip
     * @param port    port
     */
    public void nio_setIPAndPORT(UdpNioClient mClient, String ip, int port) {
        mClient.setConnectAddress(new UdpAddress[]{new UdpAddress(ip, port)});
    }

    /**
     * 连接server
     *
     * @param mClient NioClient
     */
    public void nio_connect(UdpNioClient mClient) {
        mClient.connect();
    }

    /**
     * 断开连接server
     *
     * @param mClient NioClient
     */
    public void nio_disconnect(UdpNioClient mClient) {
        mClient.disconnect();
    }

    /**
     * 重新连接server
     *
     * @param mClient NioClient
     */
    public void nio_reconnect(UdpNioClient mClient) {
        mClient.reconnect();
    }

    /**
     * 发送数据
     *
     * @param mClient NioClient
     * @param src     byte数组数据
     */
    public void nio_sendData(UdpNioClient mClient, byte[] src) {
        mClient.onSendMessage(src, 0, src.length);
    }


    ///--------------------------------------------------------


    ////----------------------BIO-----------------------------

    /**
     * 初始化BioClient
     *
     * @param mConnectResultListener 连接监听器
     * @param mMessageProcessor      信息接受监听
     */
    public UdpBioClient bio_initClient(IConnectListener mConnectResultListener, BaseMessageProcessor mMessageProcessor) {
        return new UdpBioClient(mMessageProcessor, mConnectResultListener);
    }

    /**
     * 设置ip端口
     *
     * @param mClient NioClient
     * @param ip      ip
     * @param port    port
     */
    public void bio_setIPAndPORT(UdpBioClient mClient, String ip, int port) {
        mClient.setConnectAddress(new UdpAddress[]{new UdpAddress(ip, port)});
    }

    /**
     * 连接server
     *
     * @param mClient NioClient
     */
    public void bio_connect(UdpBioClient mClient) {
        mClient.connect();
    }

    /**
     * 断开连接server
     *
     * @param mClient NioClient
     */
    public void bio_disconnect(UdpBioClient mClient) {
        mClient.disconnect();
    }

    /**
     * 重新连接server
     *
     * @param mClient NioClient
     */
    public void bio_reconnect(UdpBioClient mClient) {
        mClient.reconnect();
    }

    /**
     * 发送数据
     *
     * @param mClient NioClient
     * @param src     byte数组数据
     */
    public void bio_sendData(UdpBioClient mClient, byte[] src) {
        mClient.onSendMessage(src, 0, src.length);
    }


    ///--------------------------------------------------------


}
